using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SchoolsBI;

public partial class Materias_MateriasABM : System.Web.UI.Page
{
    static Materia oMateria = new Materia();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            MakeCbo();
            FillGrid(); }
    }

    private void MakeCbo()
    {
        for (int iOrd = 1; iOrd <= 100; iOrd++) cboOrden.Items.Add(iOrd.ToString());
    }
    protected void cmdAccion_Click(object sender, EventArgs e)
    {
        Save();
        FillGrid();
        clearFiels();

    }

    private void clearFiels()
    {
        txtIdMateria.Text = "";
        txtMateriaDesc.Text = "";
        CboTipoMateria.Value = "-1";
        cboOrden.Text = "1";
        CboTipoIdioma.Value = "-1";
        CboTipoPlanilla.Value = "-1";
        txtMateriaBoletin.Text = "";
    }
    private void FillGrid()
    {
        GridView1.DataSource = Materia.Tabla("Proc_materias");
        GridView1.DataKeyNames = new string[] { "IdMateria" };
        GridView1.AutoGenerateColumns = false;
        GridView1.DataBind();
        cmdAccion.Text = "Nuevo";


    }

    void Save()
    {
        switch (cmdAccion.Text)
        {
            case "Nuevo":
                FormToObjet();
                oMateria.Crear();
                break;
            case "Modificar":
                FormToObjet();
                oMateria.Actualizar();
                break;
            case "Eliminar":
                oMateria.Borrar();
                break;
        }
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

        oMateria = Materia.Buscar("IdMateria=" + GridView1.SelectedValue.ToString());
        showData();
        cmdAccion.Text = "Modificar";
    }

    void showData()
    {
        ObjetToForm();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        oMateria = Materia.Buscar("IdMateria=" + GridView1.DataKeys[e.RowIndex].Value);
        showData();
        cmdAccion.Text = "Eliminar";
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    void FormToObjet()
    {
        //oMateria.IdMateria = txtIdMateria.Text;
        oMateria.MateriaDesc = txtMateriaDesc.Text;
        oMateria.TipoMateria = CboTipoMateria.Value;
        oMateria.Orden =  Convert.ToInt32(cboOrden.Text);
        oMateria.IdIdioma = CboTipoIdioma.ValueInt;
        oMateria.idTipoPlanilla = CboTipoPlanilla.ValueInt;
        oMateria.MateriaBoletin = txtMateriaBoletin.Text;	
    }
    void ObjetToForm()
    {
        txtIdMateria.Text = oMateria.IdMateria.ToString();
        txtMateriaDesc.Text = oMateria.MateriaDesc;
        CboTipoMateria.Value = oMateria.TipoMateria;
        cboOrden.Text = oMateria.Orden.ToString();
        CboTipoIdioma.Value = oMateria.IdIdioma.ToString();
        CboTipoPlanilla.Value = oMateria.idTipoPlanilla.ToString();
        txtMateriaBoletin.Text = oMateria.MateriaBoletin;	
    }
}
